<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\GoodsSku;

/**
 * 商品 SKU DAO
 *
 * @author 谢声强(861167322@qq.com)
 * @package App\Dao
 */
class GoodsSkuDao extends Dao
{
    use DaoTrait;
    /**
     *  通过商品ID 创建SKU
     *
     * @param int $sku_id
     * @param string $name
     * @param float $price
     * @param int $integral
     * @param array $properties
     * @param string $picture
     * @return mixed
     */
    public function loginByPhoneOrCreate(int $sku_id, string $name, float $price, int $integral, array $properties, string $picture)
    {
        return GoodsSku::query()->firstOrCreate(['goods_id' => $sku_id], [
            'goods_id' => $sku_id,
            'name' => $name,
            'price' => $price,
            'integral' => $integral,
            'properties' => $properties,
            'picture' => $picture
        ]);
    }

    /**
     * 通过 id 获取 sku
     *
     * @param array $map
     * @param bool $abnormal
     * @return mixed
     */
    public function first(array $map, bool $abnormal = true)
    {
        $first = GoodsSku::query()->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })->first();

        if (!$first && $abnormal === true) {
            $this->error('CART.SKU_NOT_EXIST');
        }

        return $first;
    }

    /**
     * 获取SKU详情
     *
     * @param int $goods_id
     * @param string $properties
     * @return mixed
     */
    public function getDetailByProperties(int $goods_id, string $properties)
    {
        return GoodsSku::query()
            ->where('goods_id', $goods_id)
            ->where('properties', $properties)
            ->select('id', 'name', 'price', 'integral', 'stock', 'weight', 'sales', 'picture')
            ->first();
    }

    /**
     * 减库存
     *
     * @param int $id
     * @param int $num
     * @return int
     */
    public function decreaseStock(int $id, int $num)
    {
        if ($num <= 0) $this->error();

        return GoodsSku::query()->where('id', $id)->where('stock', '>=', $num)->decrement('stock', $num);

    }

    /**
     * 加库存
     *
     * @param int $id
     * @param int $num
     * @return int
     */
    public function addStock(int $id, int $num)
    {
        if ($num <= 0) $this->error();

        return GoodsSku::query()->where('id', $id)->increment('stock', $num);
    }

    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return GoodsSku::class;
    }
    /**
     * 获取Sku列表通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getSkuByMap(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

}